//
//  WelcomeView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/12.
//

import SwiftUI

struct WelcomeView: View {
    @StateObject var viewModel = WelcomeViewModel()
    @State var selectColor = "ED5C41"
    @State var isGuide : Bool = false

    var body: some View {
        ZStack {
            if !AppStorageManager.shared.isGuide {
                guideView
            }
            if (AppStorageManager.shared.isGuide || isGuide) && viewModel.configStatus > 0 && viewModel.userStatus > 0 {
                Main()
            }
        }.onAppear{
            themeManager.setDefaultTheme()
            if !viewModel.isInit {
                getData()
                viewModel.isInit = true
            }
        }
    }
    
    func getData() {
        if viewModel.configStatus == 0 || viewModel.userStatus == 0 {
            showLog("启动界面初始化数据")
            viewModel.getData()
            viewModel.getUser()
        }
    }
}

extension WelcomeView{
    private var guideView : some View {
        VStack {
            Spacer()
            ZStack {
                Image(.launcherGuide1)
                    .resizable()
                    .aspectRatio(contentMode: .fit) // 保持图片的宽高比，同时适应 frame 的大小
                    .frame(maxWidth: .infinity)
                    .padding(.horizontal,46)
                Image(.launcherGuide2)
                    .resizable()
                    .aspectRatio(contentMode: .fit) // 保持图片的宽高比，同时适应 frame 的大小
                    .frame(maxWidth: .infinity)
            }
            Spacer()
            Text("无广告、免费备份、无限制日记数量")
                .font(.system(size: 16))
                .foregroundStyle(.bgNormalText2)
            Text("每一刻都值得被珍藏，每一次回溯都是一次心灵的旅行。在“时光印记”里，让时光为你停留")
                .font(.system(size: 14))
                .foregroundStyle(.bgNormalText3)
                .padding(.top,10)
            Button(action: {
                if selectColor.isEmpty {
                    showToast("请选择一款主题色")
                    return
                }
                themeManager.setThemeColor(hex: selectColor)
                //如果接口请求失败，则只为初始化状态，重新请
                if viewModel.configStatus == 2 || viewModel.userStatus == 2 {
                    viewModel.configStatus = 0
                    viewModel.userStatus = 0
                }
                AppStorageManager.shared.isGuide = true
                isGuide = true
            }) {
                Text("让我们探索")
                    .foregroundColor(.bgNormalText)
                    .font(.system(size: 16))
                    .padding(.horizontal,10)
                    .padding(.vertical,15)
                    .frame(width: 300)
            }.background(Color.hex("F8A435"))
                .cornerRadius(8)
                .padding(.top,50)
            HStack {
                Spacer()
                Text("主题色")
                    .font(.system(size: 16))
                    .foregroundStyle(.bgNormalText2)
                Spacer()
                Color.hex("ED5C41")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "ED5C41" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "ED5C41"
                    }
                Spacer()
                Color.hex("87AE7E")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "87AE7E" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "87AE7E"
                    }
                Spacer()
                Color.hex("7F83FF")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "7F83FF" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "7F83FF"
                    }
                Spacer()
                Color.hex("fece2e")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "fece2e" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "fece2e"
                    }
                Spacer()
                Color.hex("d42517")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "d42517" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "d42517"
                    }
                Spacer()
                Color.hex("c49beb")
                    .frame(width: 15,height: 15)
                    .clipShape(Circle())
                    .overlay (
                        Circle()
                            .stroke(Color.black, lineWidth: selectColor == "c49beb" ?  1 : 0)
                    ).contentShape(Rectangle())
                    .onTapGesture {
                        selectColor = "c49beb"
                    }
                Spacer()
            }.padding(.vertical, 20)
        }.padding(.horizontal,24)
            .onDisappear {
                //消失的时候在实现一次请求
                getData()
            }
    }
}

//#Preview {
//    WelcomeView()
//}

//定义预览提供者
struct WelcomeView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: WelcomeView())
    }
}
